import matplotlib.pyplot as plt
import numpy as np

N = 18 # 天线数量
f = 50 # 频率
lambda_ = 1500 / f # 波长
d = np.array([lambda_/2.0]) * np.ones([N, 1]) # 天线距离（半波长）
n = np.arange(0, N, 1).reshape([-1, 1]) # 天线索引号
# 波束形成
theta_min, theta_max, theta_step = -90, 90, 0.1
thetas = np.arange(theta_min, theta_max, theta_step).reshape([1, -1])
theta = 10 # 扫描角度
thetas = thetas / 180 * np.pi
theta = theta / 180 * np.pi

a = np.exp(1j * 2 * np.pi * np.sin(thetas) * n * d / lambda_)
w = np.exp(1j * 2 * np.pi * np.sin(theta) * n * d / lambda_)
B = np.dot(a.transpose(), np.conj(w))
B = np.abs(B) / np.max(np.abs(B))
print(f't004 B: {B.shape};')

fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(7, 2), dpi=300)
plt.subplots_adjust(wspace=0.1, hspace=0.05)

ax[0].plot(thetas.reshape([-1,1])*180/np.pi, 20 * np.log10(B.reshape([-1,1])), '--')

a = np.mat(np.exp(1j * 2 * np.pi * np.sin(theta) * n * d / lambda_))
w = np.mat(np.exp(1j * 2 * np.pi * np.sin(thetas) * n * d / lambda_))

sigma = np.transpose(np.conj(w)) * a * np.transpose(np.conj(a)) * w
sigma = np.diag(sigma)
sigma = sigma / np.max(sigma)

ax[1].plot(thetas.reshape([-1,1])*180/np.pi, 10 * np.log10(sigma))
plt.show()
